/* CVS: CobaltVault SERVER:\\halo TREE:\abode_mainline
 *       _    ____   ___  ____  _____ 
 *      / \  | __ ) / _ \|  _ \| ____|      Advanced
 *     / _ \ |  _ \| | | | | | |  _|        Behavior
 *    / ___ \| |_) | |_| | |_| | |___       Oriented
 *   /_/   \_\____/ \___/|____/|_____|      Design
 *         www.cobaltsoftware.net           Environment
 *
 * PRODUCED FOR:      University of Bath / Boeing
 * PAYMENT:           On Delivery   
 * LICENSING MODEL:   Unrestricted distribution (Post Delivery)
 * COPYRIGHT:         Client retains copyright.
 *
 * This program and all the software components herein are
 * released as-is, without warranties regarding function,
 * correctness or any other aspect of the components.
 * Steven Gray, Cobalt Software, it's subcontractors and
 * successors may not be held liable for any damage caused 
 * to computers, business or other property through use of 
 * or misuse of this software.
 *
 * Upon redistribution of the program, all notices of
 * copyrights, both of the software provider and the 
 * client must be retained.
 */
package abode.visual;


import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.DefaultListModel;

import abode.Configuration;
import abode.JAbode;
import abode.JEngineInput;

/**
 * JOptionScreen represents a panel for providing basic configurability to the
 * program, fetching configuration settings and writing changes back using a
 * small armada of widgets designed for things such as colour configuration etc.
 * 
 * @author CobaltSoftware (abode.devteam@cobaltsoftware.net)
 * @version 1.0
 */
public class JOptionsScreen extends javax.swing.JFrame {

	// Added to implement serializable properly
	private static final long serialVersionUID = 1;

	private ArrayList buttonArray = new ArrayList();

	private DefaultListModel listModel = new DefaultListModel();

	private JAbode parentFrame = null;

	/**
	 * Initialize the options screen
	 * 
	 * @param mainFrame
	 *            The frame we'return being shown on
	 */
	public JOptionsScreen(JAbode mainFrame) {
		initComponents();
		buttonsToArray();
		buttonsToForm();
		enginesToList();
		engineList.setModel(listModel);
		parentFrame = mainFrame;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code
	// ">//GEN-BEGIN:initComponents
	private void initComponents() {
		jButton6 = new javax.swing.JButton();
		jPanel5 = new javax.swing.JPanel();
		jPanel1 = new javax.swing.JPanel();
		jPanel3 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		jLabel10 = new javax.swing.JLabel();
		jLabel11 = new javax.swing.JLabel();
		jLabel14 = new javax.swing.JLabel();
		jLabel15 = new javax.swing.JLabel();
		jLabel16 = new javax.swing.JLabel();
		jLabel17 = new javax.swing.JLabel();
		jLabel18 = new javax.swing.JLabel();
		jPanel4 = new javax.swing.JPanel();
		jPanel2 = new javax.swing.JPanel();
		jLabel12 = new javax.swing.JLabel();
		jSpinner1 = new javax.swing.JSpinner();
		jLabel13 = new javax.swing.JLabel();
		jLabel19 = new javax.swing.JLabel();
		jSpinner2 = new javax.swing.JSpinner();
		jLabel20 = new javax.swing.JLabel();
		jLabel21 = new javax.swing.JLabel();
		jSpinner3 = new javax.swing.JSpinner();
		jLabel22 = new javax.swing.JLabel();
		jLabel23 = new javax.swing.JLabel();
		jSpinner4 = new javax.swing.JSpinner();
		jLabel24 = new javax.swing.JLabel();
		jPanel6 = new javax.swing.JPanel();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jPanel7 = new javax.swing.JPanel();
		jPanel8 = new javax.swing.JPanel();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		engineList = new javax.swing.JList();

		jButton6.setText("jButton6");

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Abode Options");
		setResizable(false);
		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosed(java.awt.event.WindowEvent evt) {
				formWindowClosed(evt);
			}

			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		jPanel5.setLayout(new java.awt.GridLayout(1, 0));

		jPanel1.setLayout(new java.awt.BorderLayout());

		jPanel1.setBorder(new javax.swing.border.TitledBorder("Colours"));
		jPanel1.setPreferredSize(new java.awt.Dimension(230, 267));
		jPanel3.setLayout(new java.awt.GridLayout(16, 0));

		jLabel1.setText("Background Gradient Top");
		jPanel3.add(jLabel1);

		jLabel2.setText("Background Gradient Bottom");
		jPanel3.add(jLabel2);

		jLabel3.setText("Button Border");
		jPanel3.add(jLabel3);

		jLabel4.setText("Button Border Focused");
		jPanel3.add(jLabel4);

		jLabel5.setText("Connecting Lines");
		jPanel3.add(jLabel5);

		jLabel6.setText("Highlighted");
		jPanel3.add(jLabel6);

		jLabel7.setText("Text");
		jPanel3.add(jLabel7);

		jLabel8.setText("Sub Text");
		jPanel3.add(jLabel8);

		jLabel9.setText("Action Element");
		jLabel9.setToolTipText("");
		jPanel3.add(jLabel9);

		jLabel10.setText("Action Pattern");
		jPanel3.add(jLabel10);

		jLabel11.setText("Competence");
		jPanel3.add(jLabel11);

		jLabel14.setText("Competence Element");
		jPanel3.add(jLabel14);

		jLabel15.setText("Drive Collection");
		jPanel3.add(jLabel15);

		jLabel16.setText("Drive Element");
		jPanel3.add(jLabel16);

		jLabel17.setText("Triggered Action");
		jPanel3.add(jLabel17);

		jLabel18.setText("Trigger Elements");
		jPanel3.add(jLabel18);

		jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

		jPanel4.setLayout(new java.awt.GridLayout(16, 1));

		jPanel1.add(jPanel4, java.awt.BorderLayout.EAST);

		jPanel5.add(jPanel1);

		jPanel2.setLayout(new java.awt.GridLayout(4, 4));

		jPanel2.setBorder(new javax.swing.border.TitledBorder("Environment"));
		jLabel12.setText("Default Zoom Level:");
		jPanel2.add(jLabel12);

		jPanel2.add(jSpinner1);

		jLabel13.setText("%");
		jPanel2.add(jLabel13);

		jLabel19.setText("Min Zoom Level");
		jPanel2.add(jLabel19);

		jPanel2.add(jSpinner2);

		jLabel20.setText("%");
		jPanel2.add(jLabel20);

		jLabel21.setText("Max Zoom Level");
		jPanel2.add(jLabel21);

		jPanel2.add(jSpinner3);

		jLabel22.setText("%");
		jPanel2.add(jLabel22);

		jLabel23.setText("Node Spacing");
		jPanel2.add(jLabel23);

		jPanel2.add(jSpinner4);

		jLabel24.setText("pixels");
		jPanel2.add(jLabel24);

		jPanel5.add(jPanel2);

		getContentPane().add(jPanel5, java.awt.BorderLayout.NORTH);

		jButton1.setText("Ok");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jPanel6.add(jButton1);

		jButton2.setText("Cancel");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jPanel6.add(jButton2);

		getContentPane().add(jPanel6, java.awt.BorderLayout.SOUTH);

		jPanel7.setLayout(new java.awt.BorderLayout());

		jPanel7.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.TitledBorder("Engines")));
		jPanel8.setLayout(new java.awt.GridLayout(2, 0, 10, 15));

		jButton3.setText("Add");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		jPanel8.add(jButton3);

		jButton4.setText("Remove");
		jButton4.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton4ActionPerformed(evt);
			}
		});

		jPanel8.add(jButton4);

		jPanel7.add(jPanel8, java.awt.BorderLayout.EAST);

		jScrollPane1.setViewportView(engineList);

		jPanel7.add(jScrollPane1, java.awt.BorderLayout.CENTER);

		getContentPane().add(jPanel7, java.awt.BorderLayout.CENTER);

		pack();
	}

	// </editor-fold>//GEN-END:initComponents

	private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton4ActionPerformed
		listModel.remove(engineList.getSelectedIndex());
		engineList.repaint();
	}// GEN-LAST:event_jButton4ActionPerformed

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton3ActionPerformed
		JEngineInput input = new JEngineInput(listModel, this);
		input.show();
	}// GEN-LAST:event_jButton3ActionPerformed

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		parentFrame.setEnabled(true);
		parentFrame.transferFocus();
		dispose();
	}// GEN-LAST:event_jButton2ActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		Iterator it = buttonArray.iterator();
		while (it.hasNext()) {
			JColorSelectButton button = (JColorSelectButton) it.next();
			ArrayList al = Configuration.getByKey(button.getKey());
			al.set(1, new Integer(button.getBackground().getRed()));
			al.set(2, new Integer(button.getBackground().getGreen()));
			al.set(3, new Integer(button.getBackground().getBlue()));
		}

		int x = 0;
		ArrayList al2 = Configuration.getByKey("environment/engines");
		al2.clear();
		al2.add("engines");
		while (x < listModel.getSize()) {
			al2.add(listModel.getElementAt(x));
			x++;
		}

		Configuration.update();
		parentFrame.setEnabled(true);
		parentFrame.transferFocus();
		dispose();
		parentFrame.repaint();
		parentFrame.populateComboBox();
	}// GEN-LAST:event_jButton1ActionPerformed

	private void formWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosing
		parentFrame.setEnabled(true);
		parentFrame.transferFocus();
	}// GEN-LAST:event_formWindowClosing

	/**
	 * The window has been closed
	 */
	private void formWindowClosed(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosed
		// We used to do something here, but the
		// propegation of changes during refactoring
		// has meant this method is probably redunant.
		// Anyone?
	}// GEN-LAST:event_formWindowClosed

	/**
	 * Load the list of engines from the configuration file
	 */
	public void enginesToList() {
		ArrayList engines = Configuration.getByKey("environment/engines");
		Iterator iterator = engines.iterator();

		// Skip first element
		if (iterator.hasNext())
			iterator.next();

		while (iterator.hasNext())
			listModel.addElement(iterator.next().toString().replaceAll("\"", ""));
	}

	/**
	 * Add all of our buttons to an array
	 */
	public void buttonsToArray() {
		buttonArray.add(new JColorSelectButton("backgroundTop", this));
		buttonArray.add(new JColorSelectButton("backgroundBottom", this));
		buttonArray.add(new JColorSelectButton("buttonBorder", this));
		buttonArray.add(new JColorSelectButton("buttonBorderFocused", this));
		buttonArray.add(new JColorSelectButton("connectingLine", this));
		buttonArray.add(new JColorSelectButton("highlighted", this));
		buttonArray.add(new JColorSelectButton("text", this));
		buttonArray.add(new JColorSelectButton("subText", this));
		buttonArray.add(new JColorSelectButton("actionElement", this));
		buttonArray.add(new JColorSelectButton("actionPattern", this));
		buttonArray.add(new JColorSelectButton("competence", this));
		buttonArray.add(new JColorSelectButton("competenceElement", this));
		buttonArray.add(new JColorSelectButton("driveCollection", this));
		buttonArray.add(new JColorSelectButton("driveElement", this));
		buttonArray.add(new JColorSelectButton("triggeredAction", this));
		buttonArray.add(new JColorSelectButton("triggerElements", this));
	}

	public void buttonsToForm() {
		Iterator it = buttonArray.iterator();
		while (it.hasNext()) {
			jPanel4.add((JColorSelectButton) it.next());
		}
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JList engineList;

	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JButton jButton3;

	private javax.swing.JButton jButton4;

	private javax.swing.JButton jButton6;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel10;

	private javax.swing.JLabel jLabel11;

	private javax.swing.JLabel jLabel12;

	private javax.swing.JLabel jLabel13;

	private javax.swing.JLabel jLabel14;

	private javax.swing.JLabel jLabel15;

	private javax.swing.JLabel jLabel16;

	private javax.swing.JLabel jLabel17;

	private javax.swing.JLabel jLabel18;

	private javax.swing.JLabel jLabel19;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel20;

	private javax.swing.JLabel jLabel21;

	private javax.swing.JLabel jLabel22;

	private javax.swing.JLabel jLabel23;

	private javax.swing.JLabel jLabel24;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JLabel jLabel4;

	private javax.swing.JLabel jLabel5;

	private javax.swing.JLabel jLabel6;

	private javax.swing.JLabel jLabel7;

	private javax.swing.JLabel jLabel8;

	private javax.swing.JLabel jLabel9;

	private javax.swing.JPanel jPanel1;

	private javax.swing.JPanel jPanel2;

	private javax.swing.JPanel jPanel3;

	private javax.swing.JPanel jPanel4;

	private javax.swing.JPanel jPanel5;

	private javax.swing.JPanel jPanel6;

	private javax.swing.JPanel jPanel7;

	private javax.swing.JPanel jPanel8;

	private javax.swing.JScrollPane jScrollPane1;

	private javax.swing.JSpinner jSpinner1;

	private javax.swing.JSpinner jSpinner2;

	private javax.swing.JSpinner jSpinner3;

	private javax.swing.JSpinner jSpinner4;
	// End of variables declaration//GEN-END:variables

}
